package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategorySetmealMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;
    @Autowired
    CategorySetmealMapper categorySetmealMapper;
    @Autowired
    DishMapper dishMapper;
    /**
     * 根据id查询菜品套餐
     *
     * @param id
     */
    @Override
    public SetmealVO getByIdWithSetmeal(Long id) {
        //根据Id查询套餐数据
        Setmeal setmeal = setmealMapper.getById(id);
        //根据id查询套餐下的菜品数据
        List<SetmealDish> setmealDish = setmealDishMapper.getSetmealDish(id);


        //将查询到的数据封装到VO
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDish);
        return setmealVO;
        //根据id查询菜品对应的口味数据

    }

    /**
     * 新增套餐数据
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void saveWithSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        //将stmealDTO的数据赋给stmeal(字段相同的才会进行数据传递)
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //插入数据
        setmealDishMapper.insert(setmeal);

        //获取insert语句生成的主键值
        Long setmealId = setmeal.getId();

        //获取套餐与菜品相关数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        if (setmealDishes != null && setmealDishes.size() > 0 ){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
            //向菜品表插入n条数据
            setmealDishMapper.insertBatch(setmealDishes);
        }

    }

    /**
     *根据分类id查询菜品
     * @param id
     * @return */
    @Override
    public List<Dish> queryDishById(Long id) {
        List<Dish> dishes = categorySetmealMapper.selectDishById(id);

        return dishes;
    }

    /**
     * 分页查询套餐数据
     * @Param setmealPageQueryDTO
     * @return
     * */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //开启分页查询
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);


        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 套餐起售停售修改
     * @Param status
     * @param id
     * @return
     * */
    @Override
    public void StartOrStand(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .updateTime(LocalDateTime.now())
                .updateUser(BaseContext.getCurrentId())
                .build();
        setmealMapper.update(setmeal);
    }


    /**
     * 修改套餐数据
     * @Param setmealDTO
     * @return
     * */
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
    }

    /**
     * 删除套餐数据
     * @Param ids
     * @return
     * */
    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
        //判断当前菜品是否在售，如果在售则不可删除
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //判断套餐中是否有菜品，有则不能删除
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if (setmealIds != null && setmealIds.size() > 0){
            //套餐中含有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);

        }
        //删除套餐
        for (Long id : ids) {
            setmealMapper.deleteById(id);

            //删除套餐下套餐数据
            setmealDishMapper.deleteById(id);
        }

    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }


}

